package com.example.springboottest.example.jvm;

import java.util.HashMap;

/**
 * 1M=1024KB=1024*1024B
 */
public class StopWorldTest {
    private static final int KB = 1024;
    private static final int MB = 1024 * KB;

    public static class MyThread extends Thread {
        HashMap map = new HashMap();

        @Override
        public void run() {
            try {
                while (true) {
                    System.out.println("map.size()\t" + map.size());
                    if (map.size() >= 900) {
                        map.clear();
                        System.out.println("clean map");
                    }
                    byte[] b1;
                    for (int i = 0; i < 1024; i++) {
                        b1 = new byte[MB];
                        map.put(System.currentTimeMillis(), b1);
                    }
                    Thread.sleep(1);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static class PrintThread extends Thread {
        public static final long startTime = System.currentTimeMillis();

        @Override
        public void run() {
            try {
                while (true) {
//                    long startTime = System.currentTimeMillis() ;
                    long period = System.currentTimeMillis() - startTime;
//                    if(period>200)
//                        System.out.println("=========================");
                    System.out.println(period / 1000 + "." + period % 1000);
                    Thread.sleep(100);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }

    public static void main(String[] args) {
        MyThread myThread = new MyThread();
//        PrintThread printThread = new PrintThread();
        myThread.start();
//        printThread.start();
    }
}
